﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Web.Security;
using System.Data.SqlClient;
using System.IO;
namespace FtpClient
{
    public partial class ftpForm1 : Form
    {
        public ftpForm1()
        {
            InitializeComponent();
            LoadHistory(CombAceptName);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        //public MainForm form = null;
        public void button1_Click(object sender, EventArgs e)
        {
            
            #region 读取数据库信息
            /* string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\test1.mdb;Jet OLEDB:Database Password=123456;";
            OleDbConnection odcConnection = new OleDbConnection(strConn);   //2、打开连接 C#操作Access之按列读取mdb  
            try
            {
                odcConnection.Open();   //建立SQL查询
            }
            catch
            {
                MessageBox.Show("数据库地址错误！");
                return;
            }
            OleDbCommand odCommand = odcConnection.CreateCommand();
            //************读取数据
            string name = this.textBox1.Text.Trim();
            string PublicKey = "";
            name = FormsAuthentication.HashPasswordForStoringInConfigFile(name, "MD5");
            odCommand.CommandText = "select * from FtpUser where 用户名 = " + "'" + name + "'";   //建立读取 C#操作Access之按列读取mdb  
            OleDbDataReader odrReader = odCommand.ExecuteReader();
            odrReader.Read();
       
            try
            {
                PublicKey = odrReader[2].ToString();
                //MessageBox.Show(PublicKey);
            }
            catch
            {
                MessageBox.Show("该用户名不存在");
                odrReader.Close();
                odcConnection.Close();
                this.Close();
            }
            odrReader.Close();
            odcConnection.Close();
            MainForm form = (MainForm)this.Owner;
            form.getPub = PublicKey;
            * this.Close();*/
            #endregion
            //SQL SERVER
            string name = this.CombAceptName.Text.Trim();
            string con, sql;
            string PublicKey = "";
            con = "Server=192.168.6.1;Database=FtpInfo;uid=testA;pwd=123456";
            //con = "Server=.;Database=FtpInfo;Trusted_Connection=SSPI";
            string filename = name;
            name = FormsAuthentication.HashPasswordForStoringInConfigFile(name, "MD5");
            sql = "select * from UseInfo where 用户名 = " + "'"+name+"'";            
            SqlConnection mycon = new SqlConnection(con);
            mycon.Open();
            SqlCommand com = new SqlCommand(sql, mycon);
            //MessageBox.Show("go!");
            SqlDataReader reader = com.ExecuteReader();
            try
            {
                reader.Read();
                //MessageBox.Show(Convert.ToString(reader["用户名"] + "////" + Convert.ToString(reader["公钥"])));
                PublicKey = Convert.ToString(reader["公钥"]);
                Save(CombAceptName);
            }
            catch
            {
                MessageBox.Show("找不到该用户！");
                mycon.Close();
                return;
            }
             mycon.Close();
          
             MainForm form = (MainForm)this.Owner;
             form.getPub = PublicKey;
             form.getInfoName = filename;
             this.DialogResult=DialogResult.OK;
             this.Close();

        }
        public object GetName()
        {
            return this.CombAceptName.Text.Trim();
        }


        public void LoadHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname + ".db");
            if (File.Exists(fileName))
            {
                StreamReader r = new StreamReader(fileName, Encoding.Default);
                string name = r.ReadLine();
                while (name != null)
                {
                    CombName.AutoCompleteCustomSource.Add(name);
                    CombName.Items.Add(name);
                    name = r.ReadLine();
                }
                CombName.SelectedIndex = 0;
                r.Close();
            }
        }
        public void SaveHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname + ".db");
            StreamWriter wr = new StreamWriter(fileName, false, Encoding.Default);
            foreach (string name in CombName.AutoCompleteCustomSource)
            {
                wr.WriteLine(name);
            }
            wr.Flush();
            wr.Close();
        }
        public void Save(ComboBox CombName)
        {
            if (!CombName.AutoCompleteCustomSource.Contains(CombName.Text))
            {
                CombName.AutoCompleteCustomSource.Add(CombName.Text);
                SaveHistory(CombName);
            }
        }
        private void DeleHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname + ".db");
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            CombName.Items.Clear();
            CombName.Text = "";
        }
        private void ftpForm1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void BtDelete_Click(object sender, EventArgs e)
        {
            DeleHistory(CombAceptName);
        }
    }
}
